//
//  HeroMainController.m
//  MyDota
//
//  Created by 濮亚军 on 15/12/3.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "HeroMainController.h"

@interface HeroMainController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
//定位数组
@property(nonatomic, strong)NSArray *positionArray;

//collectionView
@property(nonatomic, strong)UICollectionView *heroMainCollection;

//所有英雄界面
@property(nonatomic, strong)HeroController *heroViewVc;

@end
static NSString *cellIdentifier = @"cell";
@implementation HeroMainController
{
    CGFloat width;
    CGFloat height;
}
#pragma mark -------设置子controller-------
-(void)setChildController
{
    //所有英雄界面
    self.heroViewVc = [[HeroController alloc]init];
    [self addChildViewController:_heroViewVc];
    self.heroViewVc.view.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - height);
    [self.view addSubview:_heroViewVc.view];
}
#pragma mark -------设置collectionView-------
-(void)setCollectionView
{
    //设置集合视图横向移动
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.heroMainCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, height) collectionViewLayout:layout];
    self.heroMainCollection.dataSource = self;
    self.heroMainCollection.delegate = self;
    self.heroMainCollection.backgroundColor = kColor;
    self.heroMainCollection.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_heroMainCollection];
    [self.heroMainCollection registerClass:[HeroMainCollectionCell class] forCellWithReuseIdentifier:cellIdentifier];
}
#pragma mark --------viewDidLoad--------
- (void)viewDidLoad {
    [super viewDidLoad];
    
    width = kScreenWidth / 6;
    height = 35 * kHProportion;
    
    //给定位数组赋值
    self.positionArray = @[@"所有", @"天辉", @"夜魇", @"核心", @"辅助", @"耐久", @"爆发",];
    NSLog(@"%ld", (unsigned long)self.positionArray.count);
    
    //加载子controller
    [self setChildController];
    
    //加载collectionView
    [self setCollectionView];
}
#pragma mark -------collectionView代理方法-------
//返回分区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//返回分区下的cell个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //数组个数
    return self.positionArray.count;
}
//返回cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HeroMainCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    NSString *string = self.positionArray[indexPath.item];
    [cell setTitle:string];
    return cell;
}
//cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(width, height);
}
//点击cell方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //判断后面的不需要移动
    if (indexPath.row < 3)
    {
        if (indexPath.row != 0)
        {
            [collectionView setContentOffset:CGPointMake((width + 10) * indexPath.row - (width + 10), 0) animated:YES];
        }
        //点击第一个item 新闻的时候 我们让偏移量 为0,0
        else
        {
            [collectionView setContentOffset:CGPointMake((width + 10) * indexPath.row, 0) animated:YES];
        }
    }
    if (indexPath.row == 3)
    {
        [collectionView setContentOffset:CGPointMake((width + 10) * indexPath.row - (width + 30), 0) animated:YES];
    }
    NSLog(@"%f", collectionView.contentOffset.x);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
